/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.screenshot;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class SaveAction extends AbstractAction {

    private File folder;
    private Component parentComponent;
    private ImageComponent imageComponent;

    public SaveAction(ImageComponent imageComponent, Component parentComponent) {
        this.imageComponent = imageComponent;
        this.parentComponent = parentComponent;
        putValue(NAME, "Save Screenshot");
        putValue(SHORT_DESCRIPTION, "Save Screenshot");
        putValue(LONG_DESCRIPTION, "Save Screenshot");
    }

    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser;
        if (folder == null) {
            fileChooser = new JFileChooser();
        } else {
            fileChooser = new JFileChooser(folder);
        }
        fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.addChoosableFileFilter(new ImageFileFilter("jpg", "JPG"));
        fileChooser.addChoosableFileFilter(new ImageFileFilter("gif", "GIF"));
        fileChooser.setFileFilter(new ImageFileFilter("png", "PNG"));
        int res = fileChooser.showSaveDialog(parentComponent);
        if (res == JFileChooser.APPROVE_OPTION) {
            File f = fileChooser.getSelectedFile();
            folder = f.getParentFile();
            ImageFileFilter filter = (ImageFileFilter)fileChooser.getFileFilter();
            String ext = '.' + filter.getExt();
            if (!f.getName().endsWith(ext)) {
                f = new File(f.getAbsolutePath() + ext);
            }
            try {
                ImageIO.write(imageComponent.getImage(), ((ImageFileFilter) fileChooser.getFileFilter()).getExt(), f);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }     }

    private static class ImageFileFilter extends FileFilter {

        private String ext;
        private String desc;

        public ImageFileFilter(String extension, String description) {
            this.ext = extension;
            this.desc = description;
        }

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            return f.getName().endsWith(ext);
        }

        @Override
        public String getDescription() {
            return desc;
        }

        public String getExt() {
            return ext;
        }

    }
}
